package org.jeecg.modules.third.jg.jss.report.cfcdriverinfo.service;

import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.annotation.CurDataSource;
import org.jeecg.config.DataSourceNames;
import org.jeecg.modules.third.jg.jss.report.cfccarrierinfo.entity.CfcCarrierinfo;
import org.jeecg.modules.third.jg.jss.report.cfccarrierinfo.mapper.CfcCarrierinfoMapper;
import org.jeecg.modules.third.jg.jss.report.cfcdriverinfo.entity.CfcDriverinfo;
import org.jeecg.modules.third.jg.jss.report.cfcdriverinfo.mapper.CfcDriverinfoMapper;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 驾驶员信息
 * @Description: CFC_DRIVERINFO
 * @Author: jeecg-boot
 * @Date:   2021-11-25
 * @Version: V1.0
 */
@Service
@Slf4j
public class CfcDriverinfoService  {

    @Resource
    private CfcDriverinfoMapper driverinfoMapper;

    @Resource
    private CfcCarrierinfoMapper carrierinfoMapper;

    public String getEnterpriseCode() {
        return enterpriseCode;
    }

    public void setEnterpriseCode(String enterpriseCode) {
        this.enterpriseCode = enterpriseCode;
    }

    private String enterpriseCode;

    public CfcDriverinfo getDriverinfo(String driverid) {
        return driverinfoMapper.selectDriverinfo( driverid );
    }


    public CfcDriverinfo getDriverinfoByIdcard(String idcard) {
        return driverinfoMapper.getDriverinfoByIdcard( idcard );
    }


    public CfcDriverinfo getDriverinfoByCarrierid(String carrierid) {
        return driverinfoMapper.getDriverinfoByCarrierid( carrierid );
    }


    @Transactional
    @CurDataSource(name = DataSourceNames.SECOND)
    public boolean addDriverinfo(HyDriver driver, HyVehicle vehicle) {
        CfcCarrierinfo  carrierinfoDb = carrierinfoMapper.getCarrierinfoByIdcard(driver.getIdcard());
        if(carrierinfoDb == null){
            CfcCarrierinfo carrierinfo =  driverTOcfcCarrierinfo(driver ,vehicle);
            carrierinfoMapper.insertSelective(carrierinfo);
            carrierinfoDb = carrierinfo;
        }

       if(carrierinfoDb == null){
           return false;
       }
        CfcDriverinfo cfcDriverinfodb =   driverinfoMapper.getDriverinfoByIdcard( driver.getIdcard() );
        if(cfcDriverinfodb != null){
            return false;
        }

        CfcDriverinfo cfcDriverinfo = driverTOcfcDriverinfo(driver);
        Integer res =  driverinfoMapper.insertSelective( cfcDriverinfo );
        return res > 0 ? true : false ;
    }


    public int deleteDriverinfo(String driverid) {
        return driverinfoMapper.deleteDriverinfo( driverid );
    }


    public CfcCarrierinfo driverTOcfcCarrierinfo(HyDriver driver, HyVehicle vehicle){
        if (driver == null || vehicle == null) {
            return null;
        }

        CfcCarrierinfo carrierinfo = new CfcCarrierinfo();
        carrierinfo.setPlatformcode(this.getEnterpriseCode());
        carrierinfo.setCarrierid(driver.getId());
        carrierinfo.setCarriertype("1");
        if (vehicle.getOwner().contains("公司")) {
            carrierinfo.setCarriername(vehicle.getOwner() + "(" + driver.getDriverName() + ")");
        } else {
            carrierinfo.setCarriername(driver.getDriverName());
        }

        carrierinfo.setUnifiedsocialcreditldentifier(driver.getIdcard());
        carrierinfo.setPermitnumber(
                StringUtils.isNotBlank(vehicle.getBusinessLicense()) ? vehicle.getBusinessLicense() :
                        vehicle.getRoadTransportNumber().substring(0 , 6) +"000000"
        );
        carrierinfo.setCountrysubdivisioncode(driver.getIdcard().substring(0 , 6));
        carrierinfo.setContactname(driver.getDriverName());
        carrierinfo.setContactmobiletelephonenumber(driver.getTelephone() );
        carrierinfo.setRegistrationdatetime(driver.getCreateTime());
        return carrierinfo;
    }


    public CfcDriverinfo driverTOcfcDriverinfo(HyDriver driver) {
        CfcDriverinfo cfcDriverinfo = new CfcDriverinfo();
        cfcDriverinfo.setPlatformcode(this.getEnterpriseCode());

        cfcDriverinfo.setDriverid(driver.getId());
        cfcDriverinfo.setNameofperson(driver.getDriverName());
        cfcDriverinfo.setGender("1");
        cfcDriverinfo.setIdentitydocumentnumber(driver.getIdcard());
        cfcDriverinfo.setMobiletelephonenumber(driver.getTelephone());
        cfcDriverinfo.setTelephonenumber(driver.getTelephone());
        cfcDriverinfo.setQualificationcertificatenumber(
                StringUtils.isNotEmpty(driver.getQualificationCertificate()) ? driver.getQualificationCertificate() : driver.getIdcard()
        );
        cfcDriverinfo.setVehicleclass(driver.getVehicleClass());
        cfcDriverinfo.setIssuingorganizations(driver.getIssuingOrganizations());
        cfcDriverinfo.setRegistrationdatetime(driver.getIssueStartDate());
        cfcDriverinfo.setValidperiodfrom(driver.getValidPeriodFrom());
        cfcDriverinfo.setValidperiodto(driver.getValidPeriodTo());
        cfcDriverinfo.setCarrierid(driver.getId());
        cfcDriverinfo.setRegistrationdatetime(driver.getCreateTime());
        return cfcDriverinfo;
    }

}
